import Mock from 'mockjs'

const users = [
  {
    id: 1,
    username: 'admin',
    password: '123456',
    email: 'admin@example.com',
    roleId: 1,
    roleName: '超级管理员',
    status: '1',
    createTime: '2024-01-01 00:00:00'
  },
  {
    id: 2,
    username: 'user',
    password: '123456',
    email: 'user@example.com',
    roleId: 2,
    roleName: '普通用户',
    status: '1',
    createTime: '2024-01-02 00:00:00'
  }
]

export default function() {
  // 登录接口
  Mock.mock('/api/auth/login', 'post', (options) => {
    const { username, password } = JSON.parse(options.body)
    const user = users.find(u => u.username === username && u.password === password)
    
    if (user) {
      return {
        code: 200,
        message: '登录成功',
        data: {
          token: Mock.Random.string('lower', 32),
          userInfo: {
            id: user.id,
            username: user.username,
            email: user.email,
            roleId: user.roleId,
            roleName: user.roleName
          }
        }
      }
    } else {
      return {
        code: 400,
        message: '用户名或密码错误',
        data: null
      }
    }
  })

  // 注册接口
  Mock.mock('/api/auth/register', 'post', (options) => {
    const { username, email, password } = JSON.parse(options.body)
    
    // 检查用户名是否已存在
    if (users.find(u => u.username === username)) {
      return {
        code: 400,
        message: '用户名已存在',
        data: null
      }
    }
    
    // 检查邮箱是否已存在
    if (users.find(u => u.email === email)) {
      return {
        code: 400,
        message: '邮箱已存在',
        data: null
      }
    }
    
    const newUser = {
      id: users.length + 1,
      username,
      email,
      password,
      roleId: 2,
      roleName: '普通用户',
      status: '1',
      createTime: Mock.Random.datetime()
    }
    
    users.push(newUser)
    
    return {
      code: 200,
      message: '注册成功',
      data: {
        id: newUser.id,
        username: newUser.username,
        email: newUser.email
      }
    }
  })

  // 登出接口
  Mock.mock('/api/auth/logout', 'post', () => {
    return {
      code: 200,
      message: '登出成功',
      data: null
    }
  })

  // 获取用户信息接口
  Mock.mock('/api/auth/userinfo', 'get', () => {
    // 这里应该根据token获取用户信息，暂时返回默认管理员信息
    const user = users[0]
    return {
      code: 200,
      message: '获取成功',
      data: {
        userInfo: {
          id: user.id,
          username: user.username,
          email: user.email,
          roleId: user.roleId,
          roleName: user.roleName
        },
        roles: ['admin'],
        permissions: ['user:list', 'user:create', 'user:edit', 'user:delete', 'role:list', 'role:create', 'role:edit', 'role:delete', 'permission:list', 'permission:create', 'permission:edit', 'permission:delete'],
        menuList: [
          {
            id: 1,
            name: '首页',
            path: '/dashboard',
            icon: 'House',
            sort: 1,
            type: 'menu'
          },
          {
            id: 2,
            name: '系统管理',
            path: '/system',
            icon: 'Setting',
            sort: 2,
            type: 'menu',
            children: [
              {
                id: 21,
                name: '用户管理',
                path: '/user',
                icon: 'User',
                sort: 1,
                type: 'menu'
              },
              {
                id: 22,
                name: '角色管理',
                path: '/role',
                icon: 'UserFilled',
                sort: 2,
                type: 'menu'
              },
              {
                id: 23,
                name: '权限管理',
                path: '/permission',
                icon: 'Lock',
                sort: 3,
                type: 'menu'
              }
            ]
          }
        ]
      }
    }
  })
} 